/*******************************************************************************
 * Copyright (c) 2010 Michael Doberenz.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Michael Doberenz - initial implementation
 *******************************************************************************/
package org.fuwjin.lifeguard.sample;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.fuwjin.lifeguard.ResourceFactory;

/**
 * A factory for producing sample pooled objects.
 */
public class SampleResourceFactory implements ResourceFactory<SampleWorker>{
   private final List<SampleWorker> objects = new LinkedList<SampleWorker>();
   private final Random rand;

   /**
    * Creates a new instance.
    * @param rand the random generator for producing durations for the callable.
    */
   public SampleResourceFactory(final Random rand){
      this.rand = rand;
   }

   /**
    * Returns true if all the objects which have been generated by this factory
    * are closed.
    * @return true if all the generated objects are closed, false otherwise
    */
   public boolean isClosed(){
      boolean state = true;
      for(final SampleWorker obj: objects){
         state &= obj.isClosed();
      }
      return state;
   }

   @Override
   public SampleWorker newResource() throws Exception{
      final SampleWorker obj = new SampleWorker(rand);
      objects.add(obj);
      return obj;
   }
}
